package com.jshx.sz.gdjt.db;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

/**
 * 数据库表操作方法
 * @author xiangchangan
 * @time 2013-08-29
 */
public class DataBaseUtil {
	//任务表名称
	private final static String task_list_table_name = "task_list";
	
	//任务表字段---------------------------------------------------------工单号-------任务状态-------检查人-----------审核人-----
	//------设备编号------设备名称------作业令号-----开始时间-------结束时间-------作业状态---------站名----------团队编号---部门编号---
	//----工艺单编号-----------工作类型--------检查类型--------检修周期----------检修等级----------
	public final static String[] task_list_table_clos = new String[]{"order_num", "status", "check_person", "audit_person"
		, "device_num", "device_name", "work_num", "start_time", "end_time", "work_status", "station_name", "team_num", "dept_num"
		, "process_orderNum", "work_type", "check_type", "check_invidate", "check_level"};
	
	//任务详情表1名称
	private final static String task_detail_type1_table_name = "task_detail_type2";
	
	//任务详情表1字段
	public final static String[] task_detail_type1_table_clos = new String[]{"order_num", "type", "install_address", "run_num"
		, "temperature", "humidity", "resualt", "hjr_name", "hjr_id", "resualt1", "remark1", "imgs1", "resualt2", "remark2", "imgs2"
		, "resualt3", "remark3", "imgs3", "resualt4", "remark4", "imgs4", "resualt5", "remark5", "imgs5", "resualt6", "remark6", "imgs6"
		, "resualt7", "remark7", "imgs7", "resualt8", "remark8", "imgs8", "resualt9", "remark9", "imgs9", "resualt10", "remark10", "imgs10"
		, "resualt11", "remark11", "imgs11", "resualt12", "remark12", "imgs12", "resualt13", "remark13", "imgs13", "resualt14", "remark14", "imgs14"
		, "resualt15", "remark15", "imgs15", "resualt16", "remark16", "imgs16", "resualt17", "remark17", "imgs17"};
	
	//任务详情表2名称
	private final static String task_detail_type2_table_name = "task_detail_type1";
	
	//任务详情表2字段
	public final static String[] task_detail_type2_table_clos = new String[]{"order_num", "type", "device_num", "device_type"
		, "version_num", "product_factory", "work_date", "rated_current", "rated_voltage", "install_address", "zddl1", "zdsx1"
		, "dzdl1", "dzsx1", "zddl2", "zdsx2", "dzdl2", "dzsx2", "qddlz1", "xl1_1", "xl1_2", "lxdgd1", "qddlz2", "xl2_1","xl2_2"
		, "lxdgd2", "cdvalue1", "cdimg1", "cdvalue2", "cdimg2", "cdvalue3", "cdimg3", "cdvalue4", "cdimg4", "cdvalue5", "cdimg5"
		, "remark", "resualt", "hjr_name", "hjr_id"};
	
	//互检人表名称
	private final static String user_table_name = "check_user";
	
	//互检人表字段
	public final static String[] user_table_clos = new String[]{"user_id", "user_name", "team_id"};
	
	//可读数据库对象
	private static SQLiteDatabase readDataBase;
	
	//可写数据库对象
	private static SQLiteDatabase writeDataBase;
	
	//上下文对象
	private static Context context;
	
	//数据库操纵对象
	private static DataBaseHelper helper;
	
	/**
	 * 初始化数据库操纵类,
	 */
	public static void initDBUtil(Context c)
	{
		context = c;
		helper = DataBaseHelper.getInstance(context);
		readDataBase = helper.getReadableDataBase(c);
		writeDataBase = helper.getWriteableDataBase(c);
	}
	
	/**
	 * 插入互检人数据
	 * @param data
	 * @return
	 */
	public static int insertCheckUserData(HashMap<String, String> data)
	{
		if(writeDataBase == null || data == null || data.keySet() == null
				|| data.keySet().size() < 1)
		{
			return -1;
		}
		ContentValues values = new ContentValues();
		values.put(user_table_clos[0], data.get(user_table_clos[0]));
		values.put(user_table_clos[1], data.get(user_table_clos[1]));
		values.put(user_table_clos[2], data.get(user_table_clos[2]));
		writeDataBase.insert(user_table_name, user_table_clos[0], values);
		return 0;
	}
	
	/**
	 * 根据teamId查询互检人
	 * @param teamId
	 * @return
	 */
	public static ArrayList<HashMap<String, String>> queryCheckUserData(String teamId)
	{
		if(teamId == null || "null".equals(teamId) || teamId.length() < 1 || readDataBase == null)
		{
			return null;
		}
		ArrayList<HashMap<String, String>> data = null;
		Cursor c = readDataBase.query(user_table_name, user_table_clos, user_table_clos[2] + "=?"
				, new String[]{teamId}, null, null, null);
		if(c != null && c.moveToFirst())
		{
			data = new ArrayList<HashMap<String,String>>();
			do
			{
				HashMap<String,String> map = new HashMap<String, String>();
				map.put(user_table_clos[0], c.getString(c.getColumnIndex(user_table_clos[0])));
				map.put(user_table_clos[1], c.getString(c.getColumnIndex(user_table_clos[1])));
				map.put(user_table_clos[2], c.getString(c.getColumnIndex(user_table_clos[2])));
				data.add(map);
			}while(c.moveToNext());
		}
		return data;
	}
	
	/**
	 * 插入一条数据到列表和详情
	 * @param data
	 * @return
	 */
	public static int insertTaskListAndDetail(HashMap<String, String> data)
	{
		if(data == null || data.get(task_list_table_clos[0]) == null)
		{
			return -1;
		}
		if(writeDataBase == null){
			return -1;
		}
		ContentValues contentValue = new ContentValues();
		for (int i = 0; i < task_list_table_clos.length; i++) 
		{
			if(data.get(task_list_table_clos[i]) != null)
			{
				contentValue.put(task_list_table_clos[i], data.get(task_list_table_clos[i]));
			}
		}
		//插入到列表里面
		deleteTask(new String[]{data.get(task_list_table_clos[0])});
		writeDataBase.insert(task_list_table_name, task_list_table_clos[0], contentValue);
		String process_orderNum = data.get(task_list_table_clos[13]);
		//插入到类型1的详情表中
		if(process_orderNum != null && process_orderNum.equals("1"))
		{
			contentValue = new ContentValues();
			for (int i = 0; i < task_detail_type1_table_clos.length; i++) 
			{
				if(data.get(task_detail_type1_table_clos[i]) != null)
				{
					contentValue.put(task_detail_type1_table_clos[i], data.get(task_detail_type1_table_clos[i]));
				}
			}
			writeDataBase.insert(task_detail_type1_table_name, task_detail_type1_table_clos[0], contentValue);
		}
		else if(process_orderNum != null && process_orderNum.equals("4"))
		{
			contentValue = new ContentValues();
			for (int i = 0; i < task_detail_type2_table_clos.length; i++) 
			{
				if(data.get(task_detail_type2_table_clos[i]) != null)
				{
					contentValue.put(task_detail_type2_table_clos[i], data.get(task_detail_type2_table_clos[i]));
				}
			}
			writeDataBase.insert(task_detail_type2_table_name, task_detail_type2_table_clos[0], contentValue);
		}
		return -1;
	}
	
	/**
	 * 插入一条数据到列表
	 * @param data
	 * @return
	 */
	public static int insertTaskList(HashMap<String, String> data)
	{
		if(data == null || data.get(task_list_table_clos[0]) == null)
		{
			return -1;
		}
		if(writeDataBase == null){
			return -1;
		}
		ContentValues contentValue = new ContentValues();
		for (int i = 0; i < task_list_table_clos.length; i++) 
		{
			if(data.get(task_list_table_clos[i]) != null)
			{
				contentValue.put(task_list_table_clos[i], data.get(task_list_table_clos[i]));
			}
		}
		//插入到列表里面
		writeDataBase.insert(task_list_table_name, task_list_table_clos[0], contentValue);
		return -1;
	}
	
	/**
	 * 删除任务
	 * @param order_Num_Array
	 * @return
	 */
	public static void deleteTask(String[] order_Num_Array)
	{
		if(writeDataBase == null || order_Num_Array == null || order_Num_Array.length < 1){
			return;
		}
		for (int i = 0; i < order_Num_Array.length; i++) {
			writeDataBase.delete(task_list_table_name, task_list_table_clos[0] + "=?", new String[]{order_Num_Array[i]});
			writeDataBase.delete(task_detail_type1_table_name, task_detail_type1_table_clos[0] + "=?", new String[]{order_Num_Array[i]});
			writeDataBase.delete(task_detail_type2_table_name, task_detail_type2_table_clos[0] + "=?", new String[]{order_Num_Array[i]});
		}
	}
	
	/**
	 * 查询所有数据
	 * @return
	 */
	public static ArrayList<HashMap<String, String>> queryAllTaskList()
	{
		if(readDataBase == null){
			return null;
		}
		ArrayList<HashMap<String, String>> data = new ArrayList<HashMap<String,String>>();
		Cursor cList = readDataBase.query(task_list_table_name, task_list_table_clos, null, null, null, null, null);
		if(cList != null && cList.moveToFirst())
		{
			do
			{
				HashMap<String, String> map = new HashMap<String, String>();
				for(int i = 0; i < task_list_table_clos.length; i++)
				{
					map.put(task_list_table_clos[i], cList.getString(cList.getColumnIndex(task_list_table_clos[i])));
				}
				String order_num = cList.getString(cList.getColumnIndex(task_list_table_clos[0]));
				String process_orderNum = cList.getString(cList.getColumnIndex(task_list_table_clos[13]));
				if(process_orderNum != null)
				{
					if(process_orderNum.equals("1"))
					{
						HashMap<String, String> mapTemp = queryType1TaskDetail(order_num);
						if(mapTemp != null && mapTemp.keySet() != null)
						{
							for (String key : mapTemp.keySet()) {
								map.put(key, mapTemp.get(key));
							}
						}
					}
					else if(process_orderNum.equals("4"))
					{
						HashMap<String, String> mapTemp = queryType2TaskDetail(order_num);
						if(mapTemp != null && mapTemp.keySet() != null)
						{
							for (String key : mapTemp.keySet()) {
								map.put(key, mapTemp.get(key));
							}
						}
					}
				}
				data.add(map);
			}while(cList.moveToNext());
		}
		if(cList != null)
		{
			cList.close();
		}
		return data;
	}
	

	
	/**
	 * 查询某一类型的task数据
	 * @param status
	 * @return
	 */
	public static ArrayList<HashMap<String, String>> queryStatusTaskList(String status)
	{
		if(readDataBase == null || status == null || status.length() < 1){
			return null;
		}
		ArrayList<HashMap<String, String>> data = new ArrayList<HashMap<String,String>>();
		Cursor cList = readDataBase.query(task_list_table_name, task_list_table_clos, task_list_table_clos[1] + "=?"
				, new String[]{status}, null, null, null);
		if(cList != null && cList.moveToFirst())
		{
			do
			{
				HashMap<String, String> map = new HashMap<String, String>();
				for(int i = 0; i < task_list_table_clos.length; i++)
				{
					map.put(task_list_table_clos[i], cList.getString(cList.getColumnIndex(task_list_table_clos[i])));
				}
				String order_num = cList.getString(cList.getColumnIndex(task_list_table_clos[0]));
				String process_orderNum = cList.getString(cList.getColumnIndex(task_list_table_clos[13]));
				if(process_orderNum != null)
				{
					if(process_orderNum.equals("1"))
					{
						HashMap<String, String> mapTemp = queryType1TaskDetail(order_num);
						if(mapTemp != null && mapTemp.keySet() != null)
						{
							for (String key : mapTemp.keySet()) {
								map.put(key, mapTemp.get(key));
							}
						}
					}
					else if(process_orderNum.equals("4"))
					{
						HashMap<String, String> mapTemp = queryType2TaskDetail(order_num);
						if(mapTemp != null && mapTemp.keySet() != null)
						{
							for (String key : mapTemp.keySet()) {
								map.put(key, mapTemp.get(key));
							}
						}
					}
				}
				data.add(map);
			}while(cList.moveToNext());
		}
		if(cList != null)
		{
			cList.close();
		}
		return data;
	}
	
	/**
	 * 
	 * @param checkUser
	 * @param status
	 * @return
	 */
	public static ArrayList<HashMap<String, String>> queryStatusAndCheckUserTaskList(String checkUser, String status)
	{
		if(readDataBase == null || status == null || status.length() < 1
				|| checkUser == null || checkUser.length() < 1){
			return null;
		}
		ArrayList<HashMap<String, String>> data = new ArrayList<HashMap<String,String>>();
		Cursor cList = readDataBase.query(task_list_table_name
				, task_list_table_clos, task_list_table_clos[1] + "=?&" + task_list_table_clos[2] + "=?"
				, new String[]{status, checkUser}, null, null, null);
		if(cList != null && cList.moveToFirst())
		{
			do
			{
				HashMap<String, String> map = new HashMap<String, String>();
				for(int i = 0; i < task_list_table_clos.length; i++)
				{
					map.put(task_list_table_clos[i], cList.getString(cList.getColumnIndex(task_list_table_clos[i])));
				}
				String order_num = cList.getString(cList.getColumnIndex(task_list_table_clos[0]));
				String process_orderNum = cList.getString(cList.getColumnIndex(task_list_table_clos[13]));
				if(process_orderNum != null)
				{
					if(process_orderNum.equals("1"))
					{
						HashMap<String, String> mapTemp = queryType1TaskDetail(order_num);
						if(mapTemp != null && mapTemp.keySet() != null)
						{
							for (String key : mapTemp.keySet()) {
								map.put(key, mapTemp.get(key));
							}
						}
					}
					else if(process_orderNum.equals("4"))
					{
						HashMap<String, String> mapTemp = queryType2TaskDetail(order_num);
						if(mapTemp != null && mapTemp.keySet() != null)
						{
							for (String key : mapTemp.keySet()) {
								map.put(key, mapTemp.get(key));
							}
						}
					}
				}
				data.add(map);
			}while(cList.moveToNext());
		}
		if(cList != null)
		{
			cList.close();
		}
		return data;
	}
	
	
	/**
	 * 
	 * @param auditUser
	 * @param status
	 * @return
	 */
	public static ArrayList<HashMap<String, String>> queryStatusAndAuditUserTaskList(String auditUser, String status)
	{
		if(readDataBase == null || status == null || status.length() < 1
				|| auditUser == null || auditUser.length() < 1){
			return null;
		}
		ArrayList<HashMap<String, String>> data = new ArrayList<HashMap<String,String>>();
		Cursor cList = readDataBase.query(task_list_table_name, task_list_table_clos
				, task_list_table_clos[1] + "=?&" + task_list_table_clos[3] + "=?"
				, new String[]{status, auditUser}, null, null, null);
		if(cList != null && cList.moveToFirst())
		{
			do
			{
				HashMap<String, String> map = new HashMap<String, String>();
				for(int i = 0; i < task_list_table_clos.length; i++)
				{
					map.put(task_list_table_clos[i], cList.getString(cList.getColumnIndex(task_list_table_clos[i])));
				}
				String order_num = cList.getString(cList.getColumnIndex(task_list_table_clos[0]));
				String process_orderNum = cList.getString(cList.getColumnIndex(task_list_table_clos[13]));
				if(process_orderNum != null)
				{
					if(process_orderNum.equals("1"))
					{
						HashMap<String, String> mapTemp = queryType1TaskDetail(order_num);
						if(mapTemp != null && mapTemp.keySet() != null)
						{
							for (String key : mapTemp.keySet()) {
								map.put(key, mapTemp.get(key));
							}
						}
					}
					else if(process_orderNum.equals("4"))
					{
						HashMap<String, String> mapTemp = queryType2TaskDetail(order_num);
						if(mapTemp != null && mapTemp.keySet() != null)
						{
							for (String key : mapTemp.keySet()) {
								map.put(key, mapTemp.get(key));
							}
						}
					}
				}
				data.add(map);
			}while(cList.moveToNext());
		}
		if(cList != null)
		{
			cList.close();
		}
		return data;
	}
	
	/**
	 * 
	 * @param order_num
	 * @return
	 */
	public static HashMap<String, String> queryType1TaskDetail(String order_num)
	{
		HashMap<String, String> data = new HashMap<String, String>();
		Cursor cursor = readDataBase.query(task_detail_type1_table_name, task_detail_type1_table_clos, task_detail_type1_table_clos[0] + "=?"
				, new String[]{order_num}, null, null, null);
		if(cursor != null && cursor.moveToFirst())
		{
			for (int i = 0; i < task_detail_type1_table_clos.length; i++) {
				data.put(task_detail_type1_table_clos[i], cursor.getString(cursor.getColumnIndex(task_detail_type1_table_clos[i])));
			}
		}
		if(cursor != null)
		{
			cursor.close();
		}
		return data;
	}
	
	/**
	 * 
	 * @param order_num
	 * @return
	 */
	public static HashMap<String, String> queryType2TaskDetail(String order_num)
	{
		HashMap<String, String> data = new HashMap<String, String>();
		Cursor cursor = readDataBase.query(task_detail_type2_table_name, task_detail_type2_table_clos, task_detail_type2_table_clos[0] + "=?"
				, new String[]{order_num}, null, null, null);
		if(cursor != null && cursor.moveToFirst())
		{
			for (int i = 0; i < task_detail_type2_table_clos.length; i++) {
				data.put(task_detail_type2_table_clos[i], cursor.getString(cursor.getColumnIndex(task_detail_type2_table_clos[i])));
			}
		}
		if(cursor != null)
		{
			cursor.close();
		}
		return data;
	}
	
	/**
	 * 更新详情到类型1的详情表中
	 * @param data
	 */
	public static void updateType1TaskDetail(HashMap<String, String> data)
	{
		if(writeDataBase == null || data == null || data.get(task_detail_type1_table_clos[0]) == null)
		{
			return;
		}
		ContentValues contentValue = new ContentValues();
		for (int i = 0; i < task_detail_type1_table_clos.length; i++) 
		{
			if(data.get(task_detail_type1_table_clos[i]) != null)
			{
				contentValue.put(task_detail_type1_table_clos[i], data.get(task_detail_type1_table_clos[i]));
			}
		}
		Cursor c = writeDataBase.query(task_detail_type1_table_name, task_detail_type1_table_clos
				, task_detail_type1_table_clos[0] + "=?", new String[]{data.get(task_detail_type1_table_clos[0])}
				, null, null, null);
		if(c != null && c.getCount() > 0)
		{
			writeDataBase.update(task_detail_type1_table_name, contentValue, task_detail_type1_table_clos[0] + "=?"
					, new String[]{data.get(task_detail_type1_table_clos[0])});
		}
		else
		{
			writeDataBase.insert(task_detail_type1_table_name, task_detail_type1_table_clos[0], contentValue);
		}
		if(c != null)
		{
			c.close();
		}
	}
	
	/**
	 * 更新详情到类型2的详情表中
	 * @param data
	 */
	public static void updateType2TaskDetail(HashMap<String, String> data)
	{
		if(writeDataBase == null || data == null || data.get(task_detail_type2_table_clos[0]) == null)
		{
			return;
		}
		ContentValues contentValue = new ContentValues();
		for (int i = 0; i < task_detail_type2_table_clos.length; i++) 
		{
			if(data.get(task_detail_type2_table_clos[i]) != null)
			{
				contentValue.put(task_detail_type2_table_clos[i], data.get(task_detail_type2_table_clos[i]));
			}
		}
		Cursor c = writeDataBase.query(task_detail_type2_table_name, task_detail_type2_table_clos
				, task_detail_type2_table_clos[0] + "=?", new String[]{data.get(task_detail_type2_table_clos[0])}
				, null, null, null);
		if(c != null && c.getCount() > 0)
		{
			writeDataBase.update(task_detail_type2_table_name, contentValue, task_detail_type2_table_clos[0] + "=?"
					, new String[]{data.get(task_detail_type2_table_clos[0])});
		}
		else
		{
			writeDataBase.insert(task_detail_type2_table_name, task_detail_type2_table_clos[0], contentValue);
		}
		if(c != null)
		{
			c.close();
		}
	}
	
	/**
	 * 更新一个数据
	 * @param data
	 * @return
	 */
	public static int updateTask(HashMap<String, String> data)
	{
		if(data == null || data.get(task_list_table_clos[0]) == null)
		{
			return -1;
		}
		if(writeDataBase == null){
			return -1;
		}
		ContentValues contentValue = new ContentValues();
		for (int i = 0; i < task_list_table_clos.length; i++) 
		{
			if(data.get(task_list_table_clos[i]) != null)
			{
				contentValue.put(task_list_table_clos[i], data.get(task_list_table_clos[i]));
			}
		}
		//插入到列表里面
		writeDataBase.update(task_list_table_name, contentValue, task_list_table_clos[0] + "=?"
				, new String[]{data.get(task_list_table_clos[0])});
		//插入到类型1的详情表中
		String process_orderNum = data.get(task_list_table_clos[13]);
		if(process_orderNum != null && process_orderNum.equals("1"))
		{
			contentValue = new ContentValues();
			for (int i = 0; i < task_detail_type1_table_clos.length; i++) 
			{
				if(data.get(task_detail_type1_table_clos[i]) != null)
				{
					contentValue.put(task_detail_type1_table_clos[i], data.get(task_detail_type1_table_clos[i]));
				}
			}
			writeDataBase.update(task_detail_type1_table_name, contentValue, task_detail_type1_table_clos[0] + "=?"
					, new String[]{data.get(task_detail_type1_table_clos[0])});
		}
		else if(process_orderNum != null && process_orderNum.equals("4"))
		{
			contentValue = new ContentValues();
			for (int i = 0; i < task_detail_type2_table_clos.length; i++) 
			{
				if(data.get(task_detail_type2_table_clos[i]) != null)
				{
					contentValue.put(task_detail_type2_table_clos[i], data.get(task_detail_type2_table_clos[i]));
				}
			}
			writeDataBase.update(task_detail_type2_table_name, contentValue, task_detail_type2_table_clos[0] + "=?"
					, new String[]{data.get(task_detail_type2_table_clos[0])});
		}
		return -1;
	}
	
 //////////////////////////////////////////////////////////////////////
 //////////////////////////////////////////////////////////////////////
	

	/**
	 * 查询明细信息
	 * @param status
	 * @return
	 */
	public   HashMap<String, String> getLocalDataByOrderNoType(String orderNo,String dataNumber )throws Exception
	{
		//String sql=" SELECT user_id,user_name FROM check_user where team_id='"+teamId+"' ";
		String sql="";
		if(dataNumber.equals("4"))
		{
			sql="select order_num,type,device_num,device_type" +
					" 		, version_num,product_factory,work_date,rated_current,rated_voltage,install_address,zddl1,zdsx1" +
					" 		, dzdl1,dzsx1,zddl2,zdsx2,dzdl2,dzsx2,qddlz1,xl1_1,xl1_2,lxdgd1,qddlz2,xl2_1,xl2_2" +
					" 		, lxdgd2,cdvalue1,cdimg1,cdvalue2,cdimg2,cdvalue3,cdimg3,cdvalue4,cdimg4,cdvalue5,cdimg5" +
					" 		, remark,resualt,hjr_name,hjr_id" +
					" from  task_detail_type1 " +
					" where order_num='"+orderNo+"'";
		}
		else if(dataNumber.equals("1"))
		{
			sql="select order_num,type,install_address,run_num" +
					" 		, temperature,humidity,resualt,hjr_name,hjr_id,resualt1,remark1,imgs1,resualt2,remark2,imgs2" +
					" 		, resualt3,remark3,imgs3,resualt4,remark4,imgs4,resualt5,remark5,imgs5,resualt6,remark6,imgs6" +
					" 		, resualt7,remark7,imgs7,resualt8,remark8,imgs8,resualt9,remark9,imgs9,resualt10,remark10,imgs10" +
					" 		, resualt11,remark11,imgs11,resualt12,remark12,imgs12,resualt13,remark13,imgs13,resualt14,remark14,imgs14" +
					" 		, resualt15,remark15,imgs15,resualt16,remark16,imgs16,resualt17,remark17,imgs17 " +
					" from  task_detail_type2 " +
					" where order_num='"+orderNo+"'";
		}
		List list=getRecordList(sql);
		if(list!=null&&list.size()>0)
		{
			return (HashMap<String, String>)list.get(0);
		}
		 
		return null;
	}
	
	/**
	 * 查询任务
	 * @param status
	 * @return
	 */
	public   ArrayList<HashMap<String, String>> getLocalTaskList( )throws Exception
	{
		//String sql=" SELECT user_id,user_name FROM check_user where team_id='"+teamId+"' ";
		String sql=" SELECT order_num,status,team_num,check_person,audit_person,device_num,process_orderNum FROM task_list where status in ('2','3')";
		return  getRecordList(sql);
	}
	/**
	 * 更新本地手机端 任务列表
	 * @param orderNum 工单编号
	 * @param type 类型  check 待检查  2 ， examine 待审核 3， checkHis 检查历史 2， examineHis 审核历史 3
	 * @param dataNumber 工艺单编号
	 */
	public void updateTaskList(String orderNum,String type,String dataNumber)
	{
		String status="2";
		if(type.equals("examine")||type.equals("examineHis"))
		{
			status="3";
		}
		
		String sql="update task_list SET status='"+status+"' WHERE order_num='"+orderNum+"' ";
		this.exeSql(sql);
	}
	
	/**
	 * 删除互检人
	 * @param status
	 * @return
	 */
	public  void deleteCheckUser()throws Exception
	{
		//String sql=" SELECT user_id,user_name FROM check_user where team_id='"+teamId+"' ";
		String sql="  delete FROM check_user ";
		this.exeSql(sql);
	}
	/**
	 * 查询互检人
	 * @param status
	 * @return
	 */
	public static  ArrayList<HashMap<String, String>> getCheckUserData(String teamId )throws Exception
	{
		//String sql=" SELECT user_id,user_name FROM check_user where team_id='"+teamId+"' ";
		String sql=" SELECT user_id,user_name FROM check_user ";
		return getRecordList(sql);
	}
	
	/**
	 * 根据SQL语句查询列表信息
	 * @param sql
	 * @return
	 * @throws Exception
	 */
	public static  ArrayList<HashMap<String, String>> getRecordList(String sql) throws Exception
	{
		Cursor cursor = null;
		ArrayList recordList=null ;
		try {
			cursor = readDataBase.rawQuery(sql, null);
			int j = 0;
			if(cursor!=null)
			{
				recordList=new ArrayList(cursor.getCount());
			}
			while (cursor != null && cursor.moveToNext()) 
			{
				HashMap<String, String> rowInfo = new HashMap<String, String>();
				for (int i = 0; i < cursor.getColumnCount(); i++) 
				{
					rowInfo.put(cursor.getColumnName(i), cursor.getString(i));
				}
				j++;
				recordList.add(j-1, rowInfo);
			}

		} catch (Exception e) {
			throw e;
		} finally {
			if (cursor != null) {
				cursor.close();
			}
		 
		}
		return recordList;
	}
	/**
	 * 执行语句 考虑事务用数组
	 * 
	 * @param sql
	 *            执行SQL
	 */
	public void exeSql(List sqlList) {
		try {
			writeDataBase.beginTransaction();
			if (sqlList != null && sqlList.size() > 0) {
				for (int i = 0; i < sqlList.size(); i++) {
					writeDataBase.execSQL((String) sqlList.get(i));
				}
			}
			writeDataBase.setTransactionSuccessful();
		} catch (Exception e) {
			System.out.println(e);
		}

		writeDataBase.endTransaction();
	}
	/**
	 * 执行语句 考虑事务用数组
	 * 
	 * @param sql
	 *            执行SQL
	 */
	public void exeSql(String sql) {
		try {
			writeDataBase.beginTransaction();
			writeDataBase.execSQL(sql);
			writeDataBase.setTransactionSuccessful();
		} catch (Exception e) {
			System.out.println(e);
		}

		writeDataBase.endTransaction();
	}
	 
}
